import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import LoginView from "@/views/LoginView.vue";
import UserTable from "@/views/UserTable.vue";
import PageError from "@/components/PageError.vue";
import BedMap from "@/views/BedMap.vue";
import NursingProjectTable from "@/components/nursing/nursingProject/NursingProjectTable.vue";
import NursingLevelTable from "@/components/nursing/nursingLevel/NursingLevelTable.vue";
import ClientNursingTable from "@/components/nursing/clientNursing/ClientNursingTable.vue"
import BedUsageTable from "@/views/BedUsageTable.vue";
import Checkin from "@/views/CheckinList.vue";
import Checkout from "@/views/CheckoutList.vue";
import Leave from "@/views/LeaveList.vue";
import CaretakerManage from "@/components/caretaker/caretakerManage/CaretakerManage.vue";
import ServiceFocus from "@/components/caretaker/serviceFocus/ServiceFocus.vue"
import DashBoard from "@/views/HomeDashboard.vue";
import MealItemTable from "@/components/meal/mealItem/MealItemTable.vue";
import MealScheduleTable from "@/components/meal/mealSchedule/MealScheduleTable.vue";
import ClientMealConfig from "@/components/meal/mealConfig/ClientMealConfig.vue";
import clientRecord from "@/components/nursing/nursingRecord/clientRecord.vue";
import ClientForm from "@/components/ClientForm.vue"

const routes = [
  // {
  //   path: '/',
  //   name: 'home',
  //   component: HomeView
  // },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
  {
    path: "/",
    redirect: "/homeView",
  },
  {
    path: "/homeView",
    component: HomeView,
    redirect: "/homeView/dashBoard",
    children: [
      {
        path: "/homeView/userManage",
        component: UserTable,
      },{
        path: "/homeView/bedManage",
        redirect: "/homeView/bedManage/bedMap",
        children: [
          {
            path: "/homeView/bedManage/bedMap",
            component: BedMap,
          }, {
            path: "/homeView/bedManage/bedUsageTable",
            component: BedUsageTable,
          }
        ]
      }, {
        path: "/homeView/clientManage",
        redirect: "/homeView/clientManage/clientForm",
        children: [
          {
            path: "/homeView/clientManage/checkin",
            component: Checkin,
          }, {
            path: "/homeView/clientManage/checkout",
            component: Checkout,
          }, {
            path: "/homeView/clientManage/leave",
            component: Leave,
          }, {
            path: '/homeView/clientManage/clientForm',
            component: ClientForm,
            meta: { title: "客户信息总览" }
          }
        ]
      }, {
        path: "/homeView/nursingManage",
        redirect: "/homeView/nursingManage/nursingProjectTable",
        children: [
          {
            path: "/homeView/nursingManage/nursingProjectTable",
            component: NursingProjectTable,
            meta: { title: "护理项目" },
          }, {
            path: "/homeView/nursingManage/nursingLevelTable",
            component: NursingLevelTable,
            meta: { title: "护理级别" },
          }, {
            path: "/homeView/nursingManage/clientNursingTable",
            component: ClientNursingTable,
            meta: { title: "客户护理配置" },
          }, {
            path: "/homeView/nursingManage/clientRecord",
            component: clientRecord,
            meta: { title: "客户护理记录" },
          }
        ]
      }, {
        path: "/homeView/healthManage",
        redirect: "/homeView/healthManage/caretakerManage",
        children: [
          {
            path: "/homeView/healthManage/caretakerManage",
            component: CaretakerManage,
            meta: { title: "设置服务对象" },
          },
          {
            path: "/homeView/healthManage/serviceFocus",
            component: ServiceFocus,
            meta: { title: "服务关注" },
          },
        ]
      }, {
        path: "/homeView/mealManage",
        redirect: "/homeView/mealManage/mealItemTable",
        children: [
          {
            path: "/homeView/mealManage/mealItemTable",
            component: MealItemTable,
            meta: { title: "菜品管理" },
          },
          {
            path: '/homeView/mealManage/mealScheduleTable',
            component: MealScheduleTable,
            meta: { title: "膳食日历" },
          },
          {
            path: '/homeView/mealManage/clientMealConfig',
            component: ClientMealConfig,
            meta: { title: "客户膳食配置" },
          }
        ]
      },{
        path: "/homeView/dashBoard",
        component: DashBoard,
      }
    ],
  },
  {
    path: "/login",
    component: LoginView,
  }, {
    path: "/error",
    component: PageError,
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 添加全局路由守卫
router.beforeEach((to, from, next) => {
  //创建守卫规则集合(这里表示'/home'与'/news'路径是需要权限验证的)
  const nextRoute = ["/login", "/error"];

  let result = sessionStorage.getItem('token')
  // 判断to.path(要跳转的路径)是否是需要权限验证的
  if (nextRoute.indexOf(to.path) == -1) {
    if (result == null || result == undefined) {
      router.push({
        path: "/login",
      });
      //location.reload();
    }
  }
  next(); //必须要有
});

export default router;